package com.lock.immersive.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lock.immersive.annotation.AuthCheck;
import com.lock.immersive.exception.BusinessException;
import com.lock.immersive.mapper.UserMapper;
import com.lock.immersive.pojo.domain.User;
import com.lock.immersive.pojo.dto.UserAccountDto;
import com.lock.immersive.pojo.dto.UserDto;
import com.lock.immersive.pojo.enums.Identity;
import com.lock.immersive.service.UserService;
import com.lock.immersive.utils.ErrorCode;
import com.lock.immersive.utils.JWT;
import com.lock.immersive.utils.ResponseApi;
import io.swagger.annotations.ApiModel;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.TimeUnit;


@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    public UserService userService;
    @Resource
    public UserMapper userMapper;

    @PostMapping("/register")
    public ResponseApi<Object> register(@RequestBody UserDto userDto) {
        return userService.register(userDto);
    }

    @PostMapping("/login")
    public ResponseApi<Object> login(@RequestBody UserDto userDto) {
        return userService.login(userDto);
    }

    @GetMapping("/sendSMS")
    public ResponseApi<Object> sendSMS(@RequestParam String phone) {
        return userService.sendSMS(phone);
    }

    @PostMapping("/updateUserInfo")
    @AuthCheck
    public ResponseApi<Object> updateUserInfo(@RequestBody User user) {
        return userService.updateUserInfo(user);
    }

    @GetMapping("/getTestToken")
    @AuthCheck
    public String getTestToken(@RequestParam String id) {
        return JWT.getJWToken(id);
    }

    @GetMapping("/getUserInfo")
    @AuthCheck
    public ResponseApi<User> getUserInfo(HttpServletRequest request) {
        return ResponseApi.success(userMapper.selectById(JWT.getTokenId(request.getHeader("token"))));
    }

    @PostMapping("/banUserList")
    @AuthCheck
    public ResponseApi<Object> banUserList(@RequestBody List<String> ids) {
        List<User> users = userMapper.selectBatchIds(ids);
        users.forEach(user ->{
            user.setIdentity(Identity.BAN);
            userMapper.updateById(user);
        });
        return ResponseApi.success("拉黑成功");
    }
}
